from Devices.Chiot.BsDevs.ComDev.ComDevParser import ComDevParser
from Devices.Chiot.BsDevs.Switch.SwitchDevice import SwitchDevice

class SwitchParser(ComDevParser):
    def __ParseCom(stateValue, fullStatus):
        ret, value = SwitchParser.GetIntProperty(stateValue, "switch1", "on")
        if ret:
            fullStatus.switch1Status = value
        ret, value = SwitchParser.GetIntProperty(stateValue, "switch2", "on")
        if ret:
            fullStatus.switch2Status = value

        ret, value = SwitchParser.GetIntProperty(stateValue, "switch3", "on")
        if ret:
            fullStatus.switch3Status = value

        ret, value = SwitchParser.GetIntProperty(stateValue, "switch4", "on")
        if ret:
            fullStatus.switch4Status = value

        ret, value = SwitchParser.GetIntProperty(stateValue, "memSwitch", "on")
        if ret:
            fullStatus.memSwitchStatus = value

        ret, value = SwitchParser.GetIntProperty(stateValue, "backlight", "on")
        if ret:
            fullStatus.backlightStatus = value

    def ParseHeartbeat(msgValue, fullStatus):
        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        SwitchParser.__ParseCom(stateValue, fullStatus)
        
        SwitchParser.GetTimers(stateValue, "timer", fullStatus.timers, SwitchDevice.Timer())
        SwitchParser.GetDelay(stateValue, "delay", fullStatus.delay)
        return True

    def ParseRptStatus(msgValue, fullStatus):
        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        SwitchParser.__ParseCom(stateValue, fullStatus)
        
        return True